package com.candicom.race;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;

public class Controller implements KeyListener {

    private final Integer Left;
    private final Integer Accelerate;
    private final Integer Right;
    private final Integer Break;
    private final Set<Integer> keys = new HashSet<Integer>();
    private int pressed;

    Controller(int _left, int _accelerate, int _right, int _break) {
        Left = _left;
        Accelerate = _accelerate;
        Right = _right;
        Break = _break;
    }

    public boolean isLeft() {
        return keys.contains(Left);
    }

    public boolean isAccelerate() {
        return keys.contains(Accelerate);
    }

    public boolean isRight() {
        return keys.contains(Right);
    }

    public boolean isBreak() {
        return keys.contains(Break);
    }

    public final void keyTyped(KeyEvent e) {       
    }

    public final void keyPressed(KeyEvent e) {
        pressed = e.getKeyCode();
        if (pressed == Left || pressed == Accelerate || pressed == Right || pressed == Break) {
            keys.add((Integer) pressed);
        }
    }

    public final void keyReleased(KeyEvent e) {
        keys.remove((Integer) e.getKeyCode());
    }
}
